package psnl.bingo.bgtool.model.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import psnl.bingo.bgtool.framework.dto.UniResult;
import psnl.bingo.bgtool.model.dto.RenameSet;
import psnl.bingo.bgtool.model.util.FileTool;

import javax.servlet.http.HttpSession;

@Slf4j
@Controller
@RequestMapping("/rename")
public class Rename {
    private static boolean isDoing;
    @RequestMapping("/index")
    public String pageDefault(HttpSession session, ModelMap model) {
        return "rename";
    }

    @RequestMapping("/execute")
    @ResponseBody
    public UniResult svUpDingSet(@RequestBody RenameSet renameSet) {
        if(isDoing) {
            return UniResult.failure("已有任务在执行");
        }

        Object data = null;
        try {
            isDoing = true;
            data = FileTool.renameFilesInDir(renameSet).testNewName;
        }catch (Exception e) {

        }finally {
            isDoing = false;
        }

        return UniResult.success(data);
    }
}
